﻿using CNKI.TPI.Web.Base;
using CNKI.TPI.Web.Interface.Models;
using CNKI.TPI.Web.Search.IBLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace CNKI.TPI.Web.Interface.Areas.User.Controllers
{
    public class GetUserRecommendController : BaseApiController
    {
        private ISearchService searchService = null;

        public GetUserRecommendController(ISearchService searchService)
        {
            this.searchService = searchService;
        }

        // GET api/userrecommend/5
        public object Get(string usercode, string length)
        {
            OutputEntity result = new OutputEntity();
            try
            {
                string valstr = searchService.GetQueryByUserCode(usercode);
                
                if (valstr == "-1")
                {
                    result.Status = Status.Error;
                }
                else
                {
                    if (valstr.Contains(";"))
                    {
                        int count = int.Parse(length);
                        string[] wordList = valstr.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                        if (wordList.Count() > count)
                        {
                            valstr = wordList[0];
                            for (int i = 1; i < count; i++)
                            {
                                valstr = valstr + ";" + wordList[i];
                            }
                        }
                    }

                    result.Status = Status.Success;
                    result.data = valstr;
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
                result.Status = Status.Error;
                result.message = ex.Message;
            }

            return result;
        }

        // POST api/userrecommend
        public void Post([FromBody]string value)
        {
        }

    }
}
